package cn.itsource.org.controller;


import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DeaprtmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
/*
    springMVC
        @GetMapping  =@RequestMapping( method = {RequestMethod.GET} )
        @PostMapping
        @PatchMapping
        @PutMapping
        @DeleteMapping

 */
@RestController //@Controller +@ResponseBody
@RequestMapping("/department")
//@CrossOrigin//spring 5及以上的版本能用
public class DepartmentController {

    @Autowired
    private IDepartmentService service;

    //根据Id查询单条
    //@RequestMapping("/queryById")
    // localhost:8080/department/1
    // localhost:8080/department/2
    /*
        /{id}  作为一个占位符 /1  /2
        @PathVariable("id")  把站位符  id取出来 给    Long id  作为参数
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return  service.loadById(id);
    }
    //根据Id查询单条
    /*
        localhost:8080/department
     */
    @GetMapping
    public List<Department> queryAll(){
        return  service.loadAll();
    }


    //@RequestMapping(value = "/delete",method = RequestMethod.POST)     只是接收一个 /delete  类型为post请求的路径

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
        return AjaxResult.me();
    }
    /*
        在接收前台数据的时候如果是数组
            后台可以使用 集合   数组
        后台的实现方案
            实现方案一：循环数组，调用删除方法[垃圾实现方案]
            实现方案二：使用sql 进行批量操作
                select * from t_department where id in (12,13,14)

     */
    @PatchMapping
    public AjaxResult deletePatch(@RequestBody Long[] ids){//ids [12,13,14]
        try {
           /*
            后台的实现方案
            循环数组，调用删除方法[垃圾实现方案]
           for (Long id : ids) {
                service.delete(id);
            }*/
            service.deletePatch(ids);

        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
        return AjaxResult.me();
    }
    /*
        @ResponseBody：响应返回json格式
        @RequestBody：接收json格式的数据
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            service.saveOrUpdate(department);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
        return AjaxResult.me();
    }

    //分页和高级查询的方法
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DeaprtmentQuery query){
        System.out.println(query);

        return service.page(query);

    }


}
